<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

/**
 * 后台首页控制器
 */
class SuggestionsController extends AdminBaseController
{
    /**
     * 消息列表
     */
    public function index()
    {
        if ($_GET['uid']) {
            $condition['uid'] = $_GET['uid'];

            $User               = M('Member');
            $map['member_name'] = array("like", "%" . $condition['uid'] . "%");
            $uid                = $User->field('member_id')->where($map)->find();
            $map1['uid']        = $uid['member_id'];
        }
        //获取反馈消息的分类
        $Type         = M('Suggestions_type');
        $map['state'] = 1;
        $list         = $Type->where($map)->select();
        $type         = $this->ancestry($list, 0);

        //获取反馈的消息
        $Suggestions = M('Suggestions');

        $count       = $Suggestions->count();
        $Page        = new \Org\Nx\Page($count, 10);
        $suggestions = $Suggestions->where($map1)->order('createtime DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show        = $Page->show();
        foreach ($suggestions as $k => $v) {
            $User                   = M('Member');
            $map['member_id']       = $v['uid'];
            $user                   = $User->field('member_name')->where($map)->find();
            $suggestions[$k]['uid'] = $user['member_name'];

            $Type                     = M('Suggestions_type');
            $map['id']                = $v['title'];
            $list                     = $Type->field('name')->where($map)->find();
            $suggestions[$k]['title'] = $list['name'];

            $suggestions[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
        }

        $this->assign('suggestions', $suggestions);
        $this->assign('type', $type);
        $this->assign('page', $show);
        $this->display();
    }

    public function suggestions_handle()
    {

        //获取反馈的消息
        $Suggestions = M('Suggestions');
        $map['id']   = I('id');
        $suggestions = $Suggestions->where($map)->find();

        $User               = M('Member');
        $map['member_id']   = $suggestions['uid'];
        $user               = $User->field('member_name')->where($map)->find();
        $suggestions['uid'] = $user['member_name'];

        $Type                 = M('Suggestions_type');
        $map['id']            = $suggestions['title'];
        $list                 = $Type->field('name')->where($map)->find();
        $suggestions['title'] = $list['name'];

        $suggestions['createtime'] = date('Y-m-d H:i:s', $suggestions['createtime']);

        $this->assign('suggestions', $suggestions);
        $this->display();

    }

    public function suggestions_edit()
    {
        if (IS_POST) {
            $data = array(
                'id'        => I('id'),
                'responses' => I('editorValue'),
                'status'    => 1,
            );

            $Suggestions = M('Suggestions');
            $result      = $Suggestions->save($data);
            if ($result) {

                $this->success('回复消息成功', U('Admin/Suggestions/index'));exit;
            } else {
                $this->error('回复消息失败', U('Admin/Suggestions/index'));exit;
            }
        }

    }

    /**
     * ajax删除操作
     */
    public function suggestions_delect()
    {
        $data = array('info' => '删除失败', 'status' => '0');
        if (!isAJAX) {
            halt('请求有误');
        } else {
            $id = I('id');

            if (!empty($id)) {

                $data = M('Suggestions');
                $info = $data->delete($id);
                if ($info >= 0) {
                    $data = array('info' => '删除成功', 'status' => '1');
                } else {
                    $data = array('info' => '删除失败', 'status' => '0');
                }

            } else {
                $data = array('info' => '参数有误');
            }
            $this->ajaxReturn($data, 'json');
        }

    }

    /**
     * 分类列表
     */
    public function type()
    {

        if ($_POST['search_name']) {
            $Type         = M('Suggestions_type');
            $map['state'] = 1;
            $map['name']  = $_POST['search_name'];

            $list  = $Type->where($map)->order('sort esc')->select();
            $lists = $this->ancestry($list, 0);
        } else {
            $Type         = M('Suggestions_type');
            $map['state'] = 1;
            $list         = $Type->where($map)->order('sort')->select();
            $lists        = $this->ancestry($list, 0);
        }
        //print_r($lists);
        $this->assign('list', $lists);
        $this->display();
    }

    /**
     * 分类添加显示
     * 上级分类只显示顶级分类
     */

    public function type_add()
    {

        $Type             = M('Suggestions_type');
        $map['state']     = 1;
        $map['parent_id'] = 0;
        $list             = $Type->where($map)->select();

        //$lists=$this->ancestry($list,0);

        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 分类添加处理
     *
     */
    public function suggestions_cat_add()
    {

        if (IS_POST) {
            $data = array(
                'name'       => I('ac_name'),
                'parent_id'  => I('parent_id'),
                'sort'       => I('sort'),
                'code'       => I('code'),
                'createtime' => time(),
            );
            $Type   = M('Suggestions_type');
            $result = $Type->add($data);
            if ($result) {

                $this->success('添加分类成功', U('Admin/Suggestions/type'));exit;
            } else {
                $this->error('添加分类失败', U('Admin/Suggestions/type'));exit;
            }
        }

    }

    /**
     * 分类修改显示
     * 上级分类只显示顶级分类
     */
    public function type_edit()
    {
        $Type             = M('Suggestions_type');
        $map['state']     = 1;
        $map['parent_id'] = 0;
        $list             = $Type->where($map)->select();

        //$lists=$this->ancestry($list,0);
        $map1['id']  = I('id');
        $suggestions = $Type->where($map1)->find();

        $this->assign('list', $list);
        $this->assign('suggestions', $suggestions);
        $this->display();
    }

    /**
     * 分类修改处理
     *
     */
    public function suggestions_cat_edit()
    {

        if (IS_POST) {
            $data = array(
                'id'         => I('id'),
                'name'       => I('name'),
                'parent_id'  => I('parent_id'),
                'sort'       => I('sort'),
                'code'       => I('code'),
                'createtime' => time(),
            );
            $Type   = M('Suggestions_type');
            $result = $Type->save($data);
            if ($result) {

                $this->success('修改分类成功', U('Admin/Suggestions/type'));exit;
            } else {
                $this->error('修改分类失败', U('Admin/Suggestions/type'));exit;
            }
        }

    }

    /**
     * ajax删除操作
     */
    public function suggestions_del()
    {
        $data = array('info' => '删除失败', 'status' => '0');
        if (!isAJAX) {
            halt('请求有误');
        } else {
            $id = I('id');

            if (!empty($id)) {
                $type_id     = M('Suggestions_type')->where('parent_id=' . $id)->select();
                $suggestions = M('Suggestions')->where('title=' . $id)->select();
                if ($type_id || $suggestions) {
                    //var_dump($cat_id);exit;
                    $data = array('info' => '该分类下还有二级分类或消息，不能直接删除', 'status' => '0');
                } else {
                    $Type = M('Suggestions_type');
                    $info = $Type->delete($id);
                    if ($info >= 0) {
                        $data = array('info' => '删除成功', 'status' => '1');
                    } else {
                        $data = array('info' => '删除失败', 'status' => '0');
                    }
                }
            } else {
                $data = array('info' => '参数有误');
            }
            $this->ajaxReturn($data, 'json');
        }

    }

    //批量删除
    public function detadelclass()
    {

        $Type = M('Suggestions_type');

        $id = $_POST['id'];
        //print_r($ac_id);exit;
        foreach ($id as $key => $val) {

            $info = $Type->delete($val);

        }
        if ($info) {
            $this->success('删除消息分类完成', U('Admin/Suggestions/type'));exit;
        } else {
            $this->error('删除消息分类失败', U('Admin/Suggestions/type'));exit;
        }

    }

    //批量删除
    public function detadel()
    {

        $data = M('Suggestions');

        $id = $_POST['id'];
        //print_r($ac_id);exit;
        foreach ($id as $key => $val) {

            $info = $data->delete($val);

        }
        if ($info) {
            $this->success('删除消息完成', U('Admin/Suggestions/index'));exit;
        } else {
            $this->error('删除消息失败', U('Admin/Suggestions/index'));exit;
        }

    }

    /**
     *
     */
    public function edit_cat_sort()
    {
        $data = array('info' => '修改失败', 'status' => '0');
        $Type = M('Suggestions_type');
        if (!isAJAX) {
            halt('请求有误');

        } else {
            $id   = I('id');
            $sort = I('sort');
            if (!empty($id)) {
                $update_array['id']   = $id;
                $update_array['sort'] = $sort;
                $info                 = $Type->save($update_array);
                if ($info >= 0) {
                    $data = array('info' => '修改成功', 'status' => '1');
                } else {
                    $data = array('info' => '修改失败', 'status' => '0');
                }
            } else {
                $data = array('info' => '参数有误');
            }
            //var_dump($data);exit;
            $this->ajaxReturn($data, 'json');
        }
    }

    //递归分类
    public function ancestry($data, $pid)
    {
        static $ancestry = array();

        foreach ($data as $key => $value) {
            if ($value['parent_id'] == $pid) {
                $ancestry[] = $value;

                $this->ancestry($data, $value['id']);
            }
        }
        return $ancestry;
    }

}
